var models = require('../db');
var express = require('express');
const jwt = require('jsonwebtoken');
var uuid = require('node-uuid');
var router = express.Router();
var mysql = require('mysql');
var $sql = require('../sql');
 
// 连接数据库
var conn = mysql.createConnection(models.mysql);
 
conn.connect();
 
// 登录接口
router.post('/login', (req, res) => {
  console.log('正在登录')
  var sql = $sql.user.login;
  var params = req.body;
  conn.query(sql, [params.userName, params.passWord], function (err, result) {
    var data = JSON.parse(JSON.stringify(result))
    if (data.length === 0) {
      return res.send({
        status: 1,
        msg: "用户名或密码错误"
      })
    } else {
      let content = {
        userName: params.userName
      }
      let secretOrPrivateKey = "_jwt"; // 这是加密的key（密钥）
      let token = jwt.sign(content, secretOrPrivateKey, {
        expiresIn: 60 * 60 * 1 // 1小时过期
      });
      return res.send({
        status: 1,
        msg: "登录成功",
        userName: params.userName,
        token: token
      })
    }
  })
});
 // 首页图书
router.post('/getHello', (req, res) => {
  var sql = $sql.user.queryHello;
  var params = req.body;
  conn.query(sql, [], function (err, result) {
    var data = JSON.parse(JSON.stringify(result))
    
    return res.send({
      status: 1,
      msg: "查询成功",
      list: data
    })
  })
});
 // 首页图书详情
 router.post('/getHellolist', (req, res) => {
  var sql = $sql.user.queryHellolist;
  conn.query(sql, [req.body.id], function (err, result) {
    var data = JSON.parse(JSON.stringify(result))
    
    return res.send({
      status: 1,
      msg: "查询成功",
      list: data
    })
  })
});

 // 首页图书详情数据
 router.post('/articlelst', (req, res) => {
  var sql = $sql.user.queryarticlelst;
  conn.query(sql, [req.body.id], function (err, result) {
    var data = JSON.parse(JSON.stringify(result))
    
    return res.send({
      status: 1,
      msg: "查询成功",
      list: data
    })
  })
});

 // 首页图书详情数据
 router.post('/articlexyz', (req, res) => {
  var sql = $sql.user.queryarticlexyz;
  conn.query(sql, [req.body.id], function (err, result) {
    var data = JSON.parse(JSON.stringify(result))
    
    return res.send({
      status: 1,
      msg: "查询成功",
      list: data
    })
  })
});
 // 首页图书分类数据
 router.post('/getHellocate', (req, res) => {
  var sql = $sql.user.queryHellocate;
  conn.query(sql, [req.body.id], function (err, result) {
    var data = JSON.parse(JSON.stringify(result))
    
    return res.send({
      status: 1,
      msg: "查询成功",
      list: data
    })
  })
});
 // 图书详情评论
 router.post('/getHellopl', (req, res) => {
  var sql = $sql.user.queryHellopl;
  conn.query(sql, [req.body.id], function (err, result) {
    var data = JSON.parse(JSON.stringify(result))
    
    return res.send({
      status: 1,
      msg: "查询成功",
      list: data
    })
  })
});
// 创建用户
router.post('/adduser', (req, res) => {
  var sql = $sql.user.loginadd;
  conn.query(sql, [req.body.id,req.body.name,req.body.password], function (err, result) {
    var data = req.body;
    return res.send({
      status: 1,
      msg: "新增成功",
      data: data
    })
  })
});
 
// 创建管理员
router.post('/addadmin', (req, res) => {
  var sql = $sql.user.adminadd;
  conn.query(sql, [req.body.id,req.body.name,req.body.password], function (err, result) {
    var data = req.body;
    return res.send({
      status: 1,
      msg: "新增成功",
      data: data
    })
  })
});
 // 更新管理员
router.post('/addadminupdate', (req, res) => {
  var sql = $sql.user.adminaddupdate;
  conn.query(sql, [req.body.id,req.body.name,req.body.password], function (err, result) {
    var data = req.body;
    return res.send({
      status: 1,
      msg: "新增成功",
      data: data
    })
  })
});

 // 更新分类
 router.post('/addcateupdate', (req, res) => {
  var sql = $sql.user.cateaddupdate;
  conn.query(sql, [req.body.id,req.body.title], function (err, result) {
    var data = req.body;
    return res.send({
      status: 1,
      msg: "新增成功",
      data: data
    })
  })
});
 // 更新用户
 router.post('/adduserupdate', (req, res) => {
  var sql = $sql.user.useraddupdate;
  conn.query(sql, [req.body.id,req.body.name,req.body.password], function (err, result) {
    var data = req.body;
    return res.send({
      status: 1,
      msg: "新增成功",
      data: data
    })
  })
});
// 创建分类
router.post('/addcate', (req, res) => {
  var sql = $sql.user.cateadd;
  conn.query(sql, [req.body.id,req.body.title], function (err, result) {
    var data = req.body;
    return res.send({
      status: 1,
      msg: "新增成功",
      data: data
    })
  })
});
 // 创建图书
router.post('/addbooks', (req, res) => {
  var sql = $sql.user.bookadd;
  conn.query(sql, [req.body.id,req.body.cid,req.body.name,req.body.auth,req.body.textarea,req.body.time], function (err, result) {
    var data = req.body;
    return res.send({
      status: 1,
      msg: "新增成功",
      data: data
    })
  })
});

 // 创建图书章节
 router.post('/addbooksadd', (req, res) => {
  var sql = $sql.user.bookaddadd;
  conn.query(sql, [req.body.id,req.body.namebook,req.body.bookid], function (err, result) {
    var data = req.body;
    return res.send({
      status: 1,
      msg: "新增成功",
      data: data
    })
  })
});
// 查询用户列表
router.post('/getAdminUist', (req, res) => {
  var sql = $sql.user.queryAdminUist;
  var params = req.body;
  conn.query(sql, [], function (err, result) {
    var data = JSON.parse(JSON.stringify(result))
    
    return res.send({
      status: 1,
      msg: "查询成功",
      list: data
    })
  })
});

// 查询管理员列表
router.post('/getAdminList', (req, res) => {
  var sql = $sql.user.queryAdminList;
  var params = req.body;
  conn.query(sql, [], function (err, result) {
    var data = JSON.parse(JSON.stringify(result))
    
    return res.send({
      status: 1,
      msg: "查询成功",
      list: data
    })
  })
});


// 查询分类列表
router.post('/getAdminCate', (req, res) => {
  var sql = $sql.user.getAdminCate;
  var params = req.body;
  conn.query(sql, [], function (err, result) {
    var data = JSON.parse(JSON.stringify(result))
    
    return res.send({
      status: 1,
      msg: "查询成功",
      list: data
    })
  })
});


// 查询图书列表
router.post('/getAdminbooks', (req, res) => {
  var sql = $sql.user.queryAdminBooks;
  var params = req.body;
  conn.query(sql, [], function (err, result) {
    var data = JSON.parse(JSON.stringify(result))
    
    return res.send({
      status: 1,
      msg: "查询成功",
      list: data
    })
  })
});
// 查询图书列表章节
router.post('/getAdminbookslst', (req, res) => {
  var sql = $sql.user.queryAdminBookslst;
  var params = req.body;
  conn.query(sql, [req.body.id], function (err, result) {
    var data = JSON.parse(JSON.stringify(result))
    
    return res.send({
      status: 1,
      msg: "查询成功",
      list: data
    })
  })
});
//查找更新系统管理员
router.post('/getAdminupdateList', (req, res) => {
  var sql = $sql.user.queryAdminadminlst;
  conn.query(sql, [req.body.id], function (err, result) {
    var data = JSON.parse(JSON.stringify(result))
    
    return res.send({
      status: 1,
      msg: "查询成功",
      list: data
    })
  })
});
//查找更新用户
router.post('/getuserupdateList', (req, res) => {
  var sql = $sql.user.queryuseruserlst;
  conn.query(sql, [req.body.id], function (err, result) {
    var data = JSON.parse(JSON.stringify(result))
    
    return res.send({
      status: 1,
      msg: "查询成功",
      list: data
    })
  })
});
//查找更新分类
router.post('/getcateupdateList', (req, res) => {
  var sql = $sql.user.querycatecatelst;
  conn.query(sql, [req.body.id], function (err, result) {
    var data = JSON.parse(JSON.stringify(result))
    
    return res.send({
      status: 1,
      msg: "查询成功",
      list: data
    })
  })
});
 //留言列表
 router.post('/getcomment', (req, res) => {
  var sql = $sql.user.querycomment;
  var params = req.body;
  conn.query(sql, [], function (err, result) {
    var data = JSON.parse(JSON.stringify(result))
    
    return res.send({
      status: 1,
      msg: "查询成功",
      list: data
    })
  })
});

// 删除留言
router.post('/getcommentdel', (req, res) => {
  var sql = $sql.user.querygetcommentdel;
  conn.query(sql, [req.body.id], function (err, result) {
    var data = JSON.parse(JSON.stringify(result))
    
    return res.send({
      status: 1,
      msg: "删除成功",
      list: data
    })
  })
});
// 删除分类
router.post('/getcatedel', (req, res) => {
  var sql = $sql.user.querygetcatedel;
  conn.query(sql, [req.body.id], function (err, result) {
    var data = JSON.parse(JSON.stringify(result))
    
    return res.send({
      status: 1,
      msg: "删除成功",
      list: data
    })
  })
});
// 删除用户
router.post('/getuserdel', (req, res) => {
  var sql = $sql.user.querygetuserdel;
  conn.query(sql, [req.body.id], function (err, result) {
    var data = JSON.parse(JSON.stringify(result))
    
    return res.send({
      status: 1,
      msg: "删除成功",
      list: data
    })
  })
});
// 删除管理员
router.post('/getadminsdel', (req, res) => {
  var sql = $sql.user.querygetadminsdel;
  conn.query(sql, [req.body.id], function (err, result) {
    var data = JSON.parse(JSON.stringify(result))
    
    return res.send({
      status: 1,
      msg: "删除成功",
      list: data
    })
  })
});
module.exports = router;